package com.zmn.mcc.admin.controller.staff.staffarchives;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.logger.LoggerUtil;
import com.zmn.mcc.admin.controller.MccBaseController;
import com.zmn.mcc.business.interfaces.pic.PicUploadBService;
import com.zmn.mcc.model.dto.pic.ImageReslutDTO;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;

/**
 * 员工档案
 *
 * @author tanghongjun
 * @since 2020-05-14 11:15:18
 */
public class StaffArchivesBaseController extends MccBaseController {

    @Resource
    StaffService staffService;

    @Resource
    protected PicUploadBService picUploadBService;

    /**
     * 描述: 文件上传  （图片上传 为master模块提供文件上传）
     *
     * @param file      文件对象
     * @param directory 文件地址
     * @param elemId    元素id
     * @param timestamp 师傅id
     * @return com.zmn.common.dto.ImageUploadDTO
     * @author wangjie
     * @since 2018/10/22 10:56
     */
    @SuppressWarnings("Duplicates")
    protected ImageReslutDTO uploaderImage(MultipartFile file, String directory, String elemId, String timestamp) {
        ImageReslutDTO imageReslutDTO = new ImageReslutDTO();
        ImageReslutDTO.Data data = new ImageReslutDTO.Data();
        try {
            directory += timestamp + "/";
            String name = file.getOriginalFilename();
            int index = name.lastIndexOf(".");
            if (index == -1) {
                return null;
            }
            String suffix = name.substring(index);
            StringBuilder fileName = new StringBuilder(elemId).append(suffix);

            ResultDTO resultDTO = picUploadBService.uploadPic(directory, fileName.toString(), file.getInputStream());
            if (resultDTO.getStatusBool()) {
                imageReslutDTO.setCode(GlobalDict.RESULT_SUCCESS);
                imageReslutDTO.setMsg(GlobalConsts.SUCCESS_MSG);
                data.setSrc(picUploadBService.getPicUrl(directory, fileName.toString()));
                imageReslutDTO.setData(data);
            }
            return imageReslutDTO;
        } catch (Exception e) {
            LoggerUtil.error(this.getClass(), e.getMessage(), e);
            return new ImageReslutDTO(GlobalDict.RESULT_FAIL, "文件上传失败", null);
        }
    }

}
